package com.ruoyi.framework.web.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

/**
 * 生成验证码的帮助类
 */

public class MateUtils {

        private final Logger log = Logger.getLogger("MateUtils.class");
/** 生成随机数列表 */
//private static final char[] CODE_TABLE= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
//        'S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
private final char[] CODE_TABLE= {'0','1','2','3','4','5','6','7','8','9'};

/** 随机码默认位数-- 6位 */
private final int DEFAULT_DIGIT = 6;

/** 默认随机码个数-- 2个 */
private final int DEFAULT_NUMBER = 2;

public MateUtils() { }

/**  默认生成单个6位预约码 */
public String generateOne() {
        StringBuilder strBuild = new StringBuilder();
        for (int i = 0; i <= DEFAULT_DIGIT; i++) {
        strBuild.append(CODE_TABLE[new Random().nextInt(36)]);
        }
        return strBuild.toString();
        }

/**
 * 指定位数生成随机码，位数需大于0，否则返回默认6位随机码
 * @param digit 随机码的位数
 */
public String generateOne(int digit) {
        if (digit > 0) {
        StringBuilder strBuild = new StringBuilder();
        for (int i = 0; i <= digit; i++) {
        strBuild.append(CODE_TABLE[new Random().nextInt(10)]);
        }
        return strBuild.toString();
        }
        return generateOne();
        }

/**
 * 默认生成 2个 6位随机码
 */
public List<String> generateList() {
        List<String> randomCodes = new ArrayList<>(DEFAULT_NUMBER);
        for (int i = 0; i < DEFAULT_NUMBER; i++) {
        randomCodes.add(generateOne());
        }
        return randomCodes;
        }

/**
 * 指定生成随机码个数、位数生成随机码，位数需大于0，否则返回默认6位随机码，个数需大于1，否则默认2个
 * @param number 随机码生成个数
 * @param digit 随机码位数
 */
public List<String> generateList(int number, int digit) {
        if (number > 1) {
        List<String> randomCodes = new ArrayList<>(number);
        for (int i = 0; i < number; i++) {
        randomCodes.add(generateOne(digit));
        }
        return randomCodes;
        }
        return generateList();
        }

/**
 * 测试入口
 */
//public static void main(String[] args) {
//        MateUtils utils = new MateUtils();
//        System.out.println(utils.generateOne(5));
//        }
}
